package com.sdb.prescription.service.impl;

import java.util.Date;
import java.util.List;

import com.sdb.patients.service.ITdPatientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sdb.prescription.mapper.TdPrescriptionMapper;
import com.sdb.prescription.domain.TdPrescription;
import com.sdb.prescription.service.ITdPrescriptionService;

/**
 * 处方Service业务层处理
 * 
 * @author yyh
 * @date 2025-02-06
 */
@Service
public class TdPrescriptionServiceImpl implements ITdPrescriptionService 
{
    @Autowired
    private TdPrescriptionMapper tdPrescriptionMapper;

    @Autowired
    private ITdPatientsService tdPatientsService;

    /**
     * 查询处方
     * 
     * @param id 处方主键
     * @return 处方
     */
    @Override
    public TdPrescription selectTdPrescriptionById(Long id)
    {
        return tdPrescriptionMapper.selectTdPrescriptionById(id);
    }

    /**
     * 查询处方列表
     * 
     * @param tdPrescription 处方
     * @return 处方
     */
    @Override
    public List<TdPrescription> selectTdPrescriptionList(TdPrescription tdPrescription)
    {
        return tdPrescriptionMapper.selectTdPrescriptionList(tdPrescription);
    }

    /**
     * 新增处方
     * 
     * @param tdPrescription 处方
     * @return 结果
     */
    @Override
    public int insertTdPrescription(TdPrescription tdPrescription)
    {
        return tdPrescriptionMapper.insertTdPrescription(tdPrescription);
    }

    /**
     * 修改处方
     * 
     * @param tdPrescription 处方
     * @return 结果
     */
    @Override
    public int updateTdPrescription(TdPrescription tdPrescription)
    {
        return tdPrescriptionMapper.updateTdPrescription(tdPrescription);
    }

    /**
     * 批量删除处方
     * 
     * @param ids 需要删除的处方主键
     * @return 结果
     */
    @Override
    public int deleteTdPrescriptionByIds(Long[] ids)
    {
        return tdPrescriptionMapper.deleteTdPrescriptionByIds(ids);
    }

    /**
     * 删除处方信息
     * 
     * @param id 处方主键
     * @return 结果
     */
    @Override
    public int deleteTdPrescriptionById(Long id)
    {
        return tdPrescriptionMapper.deleteTdPrescriptionById(id);
    }

    /**
     * 保存处方
     */
    public int savePrescription(List<TdPrescription> prescriptionDrugs) {
        for (TdPrescription pdRequest : prescriptionDrugs) {
            // 创建处方单
            TdPrescription prescription = new TdPrescription();
            prescription.setId(pdRequest.getId());
            prescription.setClinicId(pdRequest.getClinicId());
            prescription.setPatientId(pdRequest.getPatientId());
            prescription.setPatientName(pdRequest.getPatientName());
            prescription.setCode(pdRequest.getCode());
            prescription.setName(pdRequest.getName());
            prescription.setPrice(pdRequest.getPrice());
            prescription.setCount(pdRequest.getCount());
            prescription.setTotalprice(pdRequest.getTotalprice());
            prescription.setUseage(pdRequest.getUseage());
            prescription.setBeizhu(pdRequest.getBeizhu());
            prescription.setCreatetime(new Date());
            prescription.setIspay('0');
            prescription.setIspayed('0');
            // 保存处方单
            tdPrescriptionMapper.save(prescription);
        }
        return 1;
    }


    public int updateChargeStatus(TdPrescription tdPrescription) {
        return tdPrescriptionMapper.updateChargeStatus(tdPrescription);
    }

    @Override
    public List<TdPrescription> getUnsentPrescriptions(TdPrescription tdPrescription) {
        return tdPrescriptionMapper.getUnsentPrescriptions(tdPrescription);
    }

    @Override
    public int payByCard(TdPrescription tdPrescription) {
        return tdPatientsService.payByCard(tdPrescription);
    }
}
